package crm.view.desktop.iFrame.views.desktopvisualizer;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.border.BevelBorder;

import crm.model.teacherPC.net.StudentPC;



public class StudentPopup extends JPopupMenu implements ActionListener {
	private static StudentPopup popup;

	private StudentPC student = null;
	static {
		popup = new StudentPopup();
	}
	public static void show(StudentPC student, Component c, int x, int y){
		popup.showPopUp(student, c, x, y);
	}
	public void showPopUp(StudentPC student,  Component c, int x, int y){
		this.student = student;
		show(c,x,y);
	}
	public StudentPopup() {
		setBorder(new BevelBorder(BevelBorder.RAISED));
		JMenuItem item = new JMenuItem("Lock");
		item.addActionListener(this);
		add(item);
		item = new JMenuItem("Remote");
		item.addActionListener(this);
		add(item);
		item = new JMenuItem("Send Message");
		item.addActionListener(this);
		add(item);
	}

	public void actionPerformed(ActionEvent ev) {
		if (ev.getActionCommand().equals("Lock")) {
			((JMenuItem)(ev.getSource())).setText("Unlock");
			student.lockScreen();
		} else if (ev.getActionCommand().equals("Remote")) {
			
			ProcessBuilder p = new ProcessBuilder(new String[]{"java" , "-jar", "vncviewer.jar", student.getIP() + ":0"});
			
			try {
				p.start();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}else if (ev.getActionCommand().equals("Send Message")){
			String message = JOptionPane.showInputDialog(null, "Message", "Enter Message", JOptionPane.OK_OPTION);
			if (message != null){
				student.sendMessage(message);
			}
		}else if (ev.getActionCommand().equals("Unlock")) {
			((JMenuItem)(ev.getSource())).setText("Lock");
			student.unlockScreen();
		}

	}
}
